package @{包路径}.@{功能包名}.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

import cn.platform.agent.frame.core.common.MessageBean;
import cn.platform.agent.frame.core.common.ResultBean;
import cn.platform.agent.frame.core.common.ResultBeanHelper;
import cn.platform.agent.frame.core.dao.Page;
import cn.platform.agent.frame.core.helper.StringHelper;
import cn.platform.agent.frame.core.helper.UUIDHelper;
import cn.platform.agent.frame.core.service.ServiceAssert;
import cn.platform.agent.frame.core.validate.IValidator;
import cn.platform.agent.smp.component.CommonComponents;
import cn.platform.agent.smp.constant.ApplicationConstant;

import @{包路径}.@{功能包名}.dao.@{功能名_大写开头}Dao;
import @{包路径}.@{功能包名}.pojo.@{功能名_大写开头};
import @{包路径}.@{功能包名}.service.I@{功能名_大写开头}Service;

@Service("@{功能名}ServiceImpl")
public class @{功能名_大写开头}ServiceImpl implements I@{功能名_大写开头}Service {

	/**
	 * @Fields UUID_IS_EMPTY : 传入的UUID为空
	 */
	private static final String UUID_IS_EMPTY = "uuid.is.empty";

	@Resource
	private @{功能名_大写开头}Dao @{功能名}Dao;

	@Resource
	@Qualifier(value = "HibernateBeanValidator")
	private IValidator hibernatebeanValidator;

	@Resource
	@Qualifier(value = "@{功能名}UniqueValidator")
	private IValidator @{功能名}UniqueValidator;

	@Override
	public ResultBean save(@{功能名_大写开头} @{功能名}) {
		if (@{功能名}.getUuid() == null) {
			@{功能名}.setUuid(UUIDHelper.getUuid());
			@{功能名}.setCreateTime(new Date());//
		}
        ServiceAssert.assertMessages(this.hibernatebeanValidator.validate(@{功能名}));
        ServiceAssert.assertMessages(this.@{功能名}UniqueValidator.validate(@{功能名}));
        this.@{功能名}Dao.insertOrUpdate(@{功能名});
        return ResultBeanHelper.success(@{功能名});
	}

	@Override
	public ResultBean delete(String uuid) {
		ServiceAssert.assertTrue(StringHelper.isNotEmpty(uuid), new MessageBean(UUID_IS_EMPTY, MessageBean.buildArgs(uuid)));
		this.@{功能名}Dao.delete(uuid);
        return ResultBeanHelper.success();
	}

	@Override
	public ResultBean get@{功能名_大写开头}(String uuid) {
		ServiceAssert.assertTrue(StringHelper.isNotEmpty(uuid), new MessageBean(UUID_IS_EMPTY, MessageBean.buildArgs(uuid)));
        @{功能名_大写开头} @{功能名} = this.@{功能名}Dao.findById(uuid);
        ServiceAssert.assertTrue(@{功能名} != null, new MessageBean("find.@{功能名}.isnull"));
        return ResultBeanHelper.success(@{功能名});
	}

    @Override
    public ResultBean lock(List<@{功能名_大写开头}> @{功能名}s) {
        ServiceAssert.assertTrue(null != @{功能名}s && @{功能名}s.size() > 0, new MessageBean("锁定失败 ! 原因: 请求参数异常!"));
        List<String> uuids = new ArrayList<>();
        for (@{功能名_大写开头} temp : @{功能名}s) {
            uuids.add(String.valueOf(temp.getUuid()));
        }
        this.@{功能名}Dao.lockBatch(uuids);
        return ResultBeanHelper.success();
    }

    @Override
    public ResultBean unlock(List<@{功能名_大写开头}> @{功能名}s) {
        ServiceAssert.assertTrue(null != @{功能名}s && @{功能名}s.size() > 0, new MessageBean("解锁失败 ! 原因: 请求参数异常!"));
        List<String> uuids = new ArrayList<>();
        for (@{功能名_大写开头} temp : @{功能名}s) {
            uuids.add(String.valueOf(temp.getUuid()));
        }
        this.@{功能名}Dao.unlockBatch(uuids);
        return ResultBeanHelper.success();
    }

    @Override
	public Page<@{功能名_大写开头}> get@{功能名_大写开头}ByPage(int page, int pageSize, Map<String, Object> params) {
		Page<@{功能名_大写开头}> page@{功能名_大写开头} = new Page<>(pageSize, page);
		page@{功能名_大写开头}.setParams(params);
		this.@{功能名}Dao.findAllByPage(page@{功能名_大写开头});
		return page@{功能名_大写开头};
	}

}
